home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************/
- /* This program is a heavily-modified version of Phil Everson's winlev8 */
- /* from the ALV v2.1 suite of public domain image processing programs for */
- /* Sun workstations. It allows interactive thresholding and contrast */
- /* -stretching of greyscale images by manipulating the lookup tables (LUTs)*/
- /* Only greyscales from 1 to 254 are permitted. The LUTs can be saved to */
- /* a file for incorporation into a rasterfile header for the image. */
- /* */
- /* cst.c */
- /* */
- /* Bryan Dawson, Keele University Geography Dept (JANET: ged08@keele.seq1) */
- /* 27/3/89 */
- /***************************************************************************/
-
- #include <suntool/sunview.h>
- #include <suntool/canvas.h>
- #include <suntool/panel.h>
- #include <stdio.h>
- #include "defs.h"
-
- #define CSTFONT "/usr/lib/fonts/fixedwidthfonts/cour.r.16"
- #define XSIZE 256
- #define YSIZE 256
- #define MaxY 254
- #define MinY 1
-
- har *progname;
-
- tatic short csticon[] =
- {
- #include "../images/icons/cst.icon"
- };
-
- EFINE_ICON_FROM_IMAGE(icon, csticon);
-
- tatic void save_proc(),getcoords(), repaint_canvas(), invert0_proc(), quit_proc(), reset_proc();
- nt invert0 = 0,fcount = 0;
- nt yval = 255, xval = 127, lolim = MinY, hilim = MaxY;
- har *progname;
- FILE * lutfile;
- Frame frame;
- anvas canvas;
- anel panel, inv_panel;
- ixfont *font;
- ixwin *pw, *pw2;
- har cmsname[CMS_NAMESIZE];
- _char red[256], ref[256];/* all LUTs the same, so red used 3 times */
-
- #ifdef STANDALONE
- ain(argc, argv, envp)
- #else
- st_main(argc, argv, envp)
- #endif
- int argc;
- char **argv;
- char **envp;
-
- {
- int i, j, x, y;
- u_char *pi;
- char *makethelabel();
-
- progname = strsave(argv[0]);
- parse_profile(&argc, argv, envp);/* link with support.c */
-
- while ((gc = getopt(argc, argv, " ")) != EOF)
- switch (gc) {
- case '?':
- errflag++;
- break;
- }
-
- if (errflag)
- error((char *) 0, "Usage: %s:\n", progname);
-
- if ((font = pf_open(CSTFONT)) == NULL) {
- fprintf(stderr, "%s : Cannot open %s\n", argv[0], CSTFONT);
- exit(1);
- }
-
- /* Initialise default grey scale ramp */
- for (i = 0; i < 256; i++) red[i] = ref[i] = i;
- red[0] = ref[0] = 255;
- red[255] = ref[255] = 0;
-
- /* create frame and canvas */
- frame = window_create(NULL, FRAME,
- FRAME_LABEL, makethelabel(),
- WIN_HEIGHT, YSIZE + 56,
- WIN_WIDTH, XSIZE + 10,
- FRAME_ICON, &icon,
- FRAME_ARGS, argc, argv,
- 0);
-
- panel = window_create(frame, PANEL, 0);
-
- inv_panel = panel_create_item(panel, PANEL_CYCLE,
- PANEL_CHOICE_STRINGS, "Normal", "Invert", 0,
- PANEL_NOTIFY_PROC, invert0_proc,
- 0);
- panel_create_item(panel, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, panel_button_image(panel, "Save", 0, 0),
- PANEL_NOTIFY_PROC, save_proc,
- 0);
- panel_create_item(panel, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, panel_button_image(panel, "Reset", 0, 0),
- PANEL_NOTIFY_PROC, reset_proc,
- 0);
- panel_create_item(panel, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, panel_button_image(panel, "Quit", 0, 0),
- PANEL_NOTIFY_PROC, quit_proc,
- 0);
-
- window_fit(panel);
- window_set(panel, WIN_WIDTH, XSIZE, 0);
-
- canvas = window_create(frame, CANVAS,
- WIN_BELOW, panel,
- WIN_X, 0,
- WIN_EVENT_PROC, getcoords,
- 0);
-
- /* get the canvas pixwin to draw into */
- pw = canvas_pixwin(canvas);
-
- /* Set colour map segment */
- pw_setcmsname(pw, "grays");
- pw_putcolormap(pw, 0, 256, red, red, red);
-
- window_set(canvas, WIN_CONSUME_PICK_EVENTS, LOC_DRAG, 0, 0);
-
- /* N.B. The Y origin is top left of window, not bottom left */
- pw_vector(pw, lolim, MaxY, hilim, MinY, PIX_SRC, 255);
- window_main_loop(frame);
- }
-
- tatic void
- epaint_canvas()
- {
- w_writebackground(pw,0,0,256,256,PIX_SRC);
- }
-
- tatic void
- nvert0_proc(item, value, event)
- Panel_item item;
- int value;
- Event *event;
- {
- egister int p;
- invert0 = value;
- or (p=0;p<256;p++) red[p] = 255 - red[p];
- setcolormap();
- }
-
- tatic void
- etcoords(canvas, event, arg)
- Canvas canvas;
- Event *event;
- caddr_t arg;
- {
- register int q;
- char *makethelabel();
-
- switch(event_id(event))
- {
- case LOC_DRAG :
- case MS_MIDDLE:
- repaint_canvas(); /* clear window */
- yval = 255 - event_y(event); /* where's the cursor ? */
- xval = event_x(event);
- if (xval < lolim) xval = lolim; /* trap over/under ranges */
- if (xval > hilim) xval = hilim;
- pw_vector(pw,lolim,255,xval,255 - yval,PIX_SRC,255);
- pw_vector(pw,xval,255 - yval,hilim,0,PIX_SRC,255);
- stretchlut(lolim,MinY,xval,yval,0);
- stretchlut(xval,yval,hilim,MaxY,yval);
- if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
- pw_putcolormap(pw, 0, 256, red, red, red);
- break;
- case MS_LEFT :
- repaint_canvas();
- lolim = event_x(event);
- if (lolim >= hilim) lolim = hilim - 1;
- pw_vector(pw,lolim,255,hilim,0,PIX_SRC,255);
- stretchlut(lolim,MinY,hilim,MaxY,0);
- if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
- pw_putcolormap(pw, 0, 256, red, red, red);
- window_set(frame,FRAME_LABEL,makethelabel(),0);
- break;
- case MS_RIGHT :
- repaint_canvas();
- hilim = event_x(event);
- if (hilim <= lolim) hilim = lolim + 1;
- pw_vector(pw,lolim,255,hilim,0,PIX_SRC,255);
- stretchlut(lolim,MinY,hilim,MaxY,0);
- if (invert0) for (q=1;q<255;q++) red[q] = 255 - red[q];
- pw_putcolormap(pw, 0, 256, red, red, red);
- window_set(frame,FRAME_LABEL,makethelabel(),0);
- break;
- } /* end of switch */
- }
-
- tretchlut(lox,loy,hix,hiy,base)
- nt lox,loy,hix,hiy,base;
- {
- nt i,rangex,rangey,temp;
- ouble a;
-
- rangex = hix - lox + 1;
- rangey = hiy - loy + 1;
- a = (double)rangey / (double)rangex; /* scaling factor */
- or (i=MinY;i<lolim;i++) red[i] = 0;
- or (i=hilim;i<MaxY;i++); red[i] = 255;
- or (i=lox;i<hix;i++)
- {
- temp = (int)((i - lox + 1) * a); /* offset & gain compensation */
- temp += base; /* for top of two-part stretch */
- if (temp < 1) temp = 1; /* avoid exteme values - used for text */
- if (temp > 254) temp = 254;
- red[i] = (unsigned char)temp;
- }
- }
-
- etcolormap()
- {
- nt i;
-
- /* Initialise variables used to set colour map */
- ed[0] = 255;
- ed[255] = 0;
- w_putcolormap(pw, 0, 256, red, red, red);
- }
-
- har *
- akethelabel()
- {
- static char buf[BUFSIZ];
- char *sprintf();
- return sprintf(buf, "Lolim = %3d Hilim = %3d", lolim, hilim);
- }
-
- tatic void
- uit_proc()
- {
- window_set(frame, FRAME_NO_CONFIRM, TRUE, 0);
- window_destroy(frame);
- }
-
- tatic void
- eset_proc()
- {
- egister int i;
-
- repaint_canvas(); /* clear window & redraw line */
- pw_vector(pw,lolim,MaxY,hilim,MinY,PIX_SRC,255);
-
- invert0 = 0; /* reset normal/invert0 toggle */
- panel_set_value(inv_panel, invert0);
-
- for (i = 0; i < 256; i++) red[i] = i; /* plain ramp */
- setcolormap();
-
- lolim = MinY; hilim = MaxY; /* default labels */
- window_set(frame, FRAME_LABEL, makethelabel(), 0);
- }
-
- tatic void
- ave_proc()
- {
- nt id;
- har fname[10];
-
- printf(fname,"clut.%03d",++fcount);
- utfile = fopen(fname,"w");
- d = fwrite(red,1,256,lutfile);
- d = fwrite(red,1,256,lutfile);
- d = fwrite(red,1,256,lutfile);
- d = fclose(lutfile);
- }
-